import Vue from "vue";
import VueRouter from "vue-router";

// 给原型对象上的push和replace方法进行重写
/*
修改路由器的型当中的push成为我们自己定义的push
*/
const originPush = VueRouter.prototype.push; // 先把原来的push函数保存一份
const originReplace = VueRouter.prototype.replace; // 先把原来的replace函数保存一份

VueRouter.prototype.push = function(location, resolve, reject) {
  // 判断如果没有指定的回调函数，通过call调用函数并使用catch处理错误
  if (resolve === undefined && reject === undefined) {
    return originPush.call(this, location).catch(() => {});
  } else {
    // 如果有指定任意回调，那么通过call调用push与回调进行函数处理
    originPush.call(this, location, resolve, reject);
  }
};

VueRouter.prototype.replace = function(location, resolve, reject) {
  // 判断如果没有指定的回调函数，通过call调用函数并使用catch处理错误
  if (resolve === undefined && reject === undefined) {
    return originReplace.call(this, location).catch(() => {});
  } else {
    // 如果有指定任意回调，那么通过call调用push与回调进行函数处理
    originReplace.call(this, location, resolve, reject);
  }
};

Vue.use(VueRouter);

import Home from "@/views/Home";
import Login from "@/views/Login";
import Register from "@/views/Register";
import Search from "@/views/Search";

export default new VueRouter({
  mode: "history",
  routes: [
    {
      name: "Home",
      path: "/home",
      component: Home,
    },
    {
      name: "Login",
      path: "/login",
      component: Login,
      meta: {
        isHidden: true,
      },
    },
    {
      name: "Register",
      path: "/register",
      component: Register,
      meta: {
        isHidden: true,
      },
    },
    {
      name: "Search",
      path: "/search/:keyword?", 
      component: Search,
      
    },
    {
      path: "*",
      component: Home,
    },
  ],
});
